package com.xihuanjun.sdk.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xihuanjun.sdk.entity.JSTInout;
import com.xihuanjun.sdk.entity.JSTOut;
import com.xihuanjun.sdk.entity.JSTPageData;
import com.xihuanjun.sdk.enums.JSTMethod;
import com.xihuanjun.sdk.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2024-09-06
 * FileName: JSTOutDao
 * Author:   溪浣君
 * Date:     2024-09-06 16:09
 * Description: 出库单操作
 */
@Component
@Slf4j
public class JSTOutDao extends BaseDao {


    /**
     * 根据内部单号查询聚水潭出库单，只返回一个已出库状态的
     * @param o_id
     * @return
     */
    public JSTOut getJSTOut(String o_id) throws IOException {
        Map map=new HashMap();
        map.put("o_ids",new String[]{o_id});
        map.put("status","Confirmed");
        List<JSTOut> jstOuts = this.jstListQuery(map, JSTMethod.JST_ORDER_OUT_SIMPLE_QUERY, new TypeReference<List<JSTOut>>() {});
        return getOneFromList(jstOuts);
    }

    /**
     * 分页查询聚水潭出库单
     * @param modifyStart
     * @param modifyEnd
     * @param shopId
     * @param status
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws IOException
     */
    public List<JSTOut> getJSTList(String modifyStart, String modifyEnd, String shopId, String status, int pageIndex, int pageSize) throws IOException {
        Map map=new HashMap();
        if(StringUtils.hasText(shopId)){
            map.put("shop_id",shopId);
        }
        if(StringUtils.hasText(modifyStart)){
            map.put("modified_begin",modifyStart);
        }
        if(StringUtils.hasText(modifyEnd)){
            map.put("modified_end",modifyEnd);
        }
        if(StringUtils.hasText(status)){
            map.put("status",status);
        }
        map.put("page_index",pageIndex);
        map.put("page_size",pageSize);
        List<JSTOut> jstOuts = this.jstListQuery(map, JSTMethod.JST_ORDER_OUT_SIMPLE_QUERY, new TypeReference<List<JSTOut>>() {});
        return jstOuts;
    }

    /**
     * 分页查询出库单（奇门）
     * @param modifyStart
     * @param modifyEnd
     * @param shopId
     * @param status
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws IOException
     */
    public List<JSTOut> getQMList(String modifyStart, String modifyEnd, String shopId, String status, int pageIndex, int pageSize) throws IOException {
        HashMap<String,String> map=new HashMap<>();
        if(StringUtils.hasText(shopId)){
            map.put("shop_id",shopId);
        }
        if(StringUtils.hasText(modifyStart)){
            map.put("start_time",modifyStart);
        }
        if(StringUtils.hasText(modifyEnd)){
            map.put("end_time",modifyEnd);
        }
        if(StringUtils.hasText(status)){
            map.put("status",status);
        }
        map.put("data_type","0");
//        map.put("o_ids","2870737");

        map.put("page_index",pageIndex+"");
        map.put("page_size",pageSize+"");
        List<JSTOut> jstOuts = this.qmListQuery(map, JSTMethod.QM_SALEOUT_LIST_QUERY,new TypeReference<List<JSTOut>>(){});
        return jstOuts;
    }

    /**
     * 根据内部单号查询聚水潭出库单（奇门），只返回一个已出库状态的
     * @param o_id
     * @return
     * @throws IOException
     */
    public JSTOut getQMOut(int o_id) throws IOException {
        HashMap map=new HashMap<>();
        map.put("status","Confirmed");
        map.put("o_ids",o_id+"");
        map.put("page_index","1");
        map.put("page_size","10");
        List<JSTOut> jstOuts = this.qmListQuery(map, JSTMethod.QM_SALEOUT_LIST_QUERY,new TypeReference<List<JSTOut>>(){});
        return getOneFromList(jstOuts);
    }
}
